/**
 * Created by aifece on 15/1/20.
 */
(function(){
  // 地址
  function AddressCurr(option)
  {
    AddressJs.call(this, option);
  }

  for (var i in AddressJs.prototype) {
    AddressCurr.prototype[i] = AddressJs.prototype[i]
  }

  AddressCurr.prototype.sltChange = function(obj)
  {

  }

  AddressCurr.prototype.setForm = function(obj)
  {
    this.showForm();

    var id =  obj.attr('data-address_id');
    if (id == 'new') {
      this.showInfo(0, 'new');
    } else {
      this.showInfo(id, 'edit');
    }
  }

  AddressCurr.prototype.showForm = function()
  {
    var _this = this;
    $(this.option.formDiv).find('input').each(function(){
      _this.deleteMsg($(this));
    });
    $(this.option.formDiv).css({ position:'fixed' });
    $(this.option.formDiv).removeClass('hidden').prev().removeClass('hidden');
    $(this.option.closeDiv).on('click', function(){
      _this.hideForm();
    });
  }

  AddressCurr.prototype.hideForm = function()
  {
    $(this.option.formDiv).addClass('hidden').prev().addClass('hidden');
    $(this.option.closeDiv).on('click', null);
  }

  // 提示信息
  AddressCurr.prototype.inputMsg =  function(obj, msg)
  {
    obj.parent().addClass('error');
    if ($(obj).closest('li').hasClass('two')) {
      $(obj).closest('li').append('<span class="manager-name2">'+msg+'</span>');
    } else {
      $(obj).closest('li').addClass('manager-curr').find('label').append('<span class="manager-name">'+msg+'</span>');
    }
  }

  AddressCurr.prototype.deleteMsg = function(obj){
    obj.parent().removeClass('error');
    if ($(obj).closest('li').hasClass('two')) {
      $(obj).closest('li').find('.manager-name2').remove();
    } else {
      $(obj).closest('li').removeClass('manager-curr').find('.manager-name').remove();
    }
  }

  AddressCurr.prototype.setInfo = function(type, id, info, receiver, address, mobile, provT, cityT, distT)
  {
    if (!id) {
      return;
    }
    var _this = this;

    if (type == 'new') {
      $(_this.option.add).after('<div class="manage-box" data-address_id="'+id+'">\
          <h3>'+receiver+'</h3>\
        <ul>\
          <li class="manage-curr">'+mobile+'</li>\
          <li>'+provT+' '+cityT+' '+distT+'</li>\
          <li>'+address+'</li>\
        </ul>\
        <a href="javascript:void(0);" class="js_addressEdit" data-address_id="'+id+'">编辑</a>\
        <a href="javascript:void(0);" class="js_addressDelete" data-address_id="'+id+'">删除</a>\
      </div>');
    } else {
      $(_this.option.listDiv).find('.manage-box').each(function() {
        if ($(this).attr('data-address_id') == id) {
          $(this).find('h3').html(receiver);
          $(this).find('ul').html('<li class="manage-curr">'+mobile+'</li>\
          <li>'+provT+' '+cityT+' '+distT+'</li>\
          <li>'+address+'</li>');
        }
      });
    }

    location.href = location.href;
  }

  $(function(){
    new AddressCurr({
      'closeDiv' : '.js_closeFormDiv'
    });

    //删除地址
    $('body').on('click', '.js_addressDelete',function(){
      var that = $(this);
      var id = $(this).attr('data-address_id');
      var url = siteUrl+'/address/delete_address';

      if (btnClick() === false) {
        return false;
      }
      showConfirm('您确定要删除当前地址', function(){
        $.post(url, { id:id }, function(json){
          if (json.info == 'ok') {
            location.href = location.href;
            //that.parent().remove();
          } else {
            showMsg(json.info, 'error');
          }
          btnClick('reset');
        }, 'json');
      })
    });

    $('.js_ADDIsDefault label').on('click', function(){
      var isDef = $(this).closest('div').hasClass('manager-on');
      if (isDef) {
        $(this).closest('div').removeClass('manager-on');
      } else {
        $(this).closest('div').addClass('manager-on');
      }
      return false;
    });
  })
})()